package com.example.administrator.myapplication;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.example.administrator.myapplication.damo.BlankFragment;
import com.example.administrator.myapplication.damo.BlankFragment2;
import com.example.administrator.myapplication.damo.BlankFragment3;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    //准备数据
    ArrayList<Fragment> fragments;
    //定义适配器
    FragmentPagerAdapter adapter;
    //应用适配器
    ViewPager viewPager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initDate();  //构建fragment
        init();
        viewPager=findViewById(R.id.vp_fragment);
        viewPager.setAdapter(adapter);
    }

    private void init() {
        adapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragments.get(position);
            }

            @Override
            public int getCount() {
                return fragments.size();
            }

        };
    }
    private void initDate() {
        fragments=new ArrayList<>();
        fragments.add(new BlankFragment());
        fragments.add(new BlankFragment3());
        fragments.add(new BlankFragment2());
    }
}
